<script setup>
import { ref, onMounted, onUnmounted } from 'vue'

const videos = ref([])
const blessings = ref([])
const displayedBlessings = ref([])
const currentIndex = ref(0)
const scrollInterval = ref(null)

// 视频数据
const videoList = [
  { id: 1, name: '视频1', thumbnail: '/VideoAndBlessings/videos/video1.mp4', video: 'video1.mp4' },
  { id: 2, name: '视频2', thumbnail: '/VideoAndBlessings/videos/video2.mp4', video: 'video2.mp4' },
  { id: 3, name: '视频3', thumbnail: '/VideoAndBlessings/videos/video3.mp4', video: 'video3.mp4' },
  { id: 4, name: '视频4', thumbnail: '/VideoAndBlessings/videos/video4.mp4', video: 'video4.mp4' }
]

// 祝福语填写表单
const newBlessing = ref({
  name: '',
  message: ''
})

// 加载祝福语数据
const loadBlessings = async () => {
  try {
    const response = await fetch('/VideoAndBlessings/blessings.json')
    const data = await response.json()
    blessings.value = data.messages
    initializeDisplayedBlessings()
  } catch (error) {
    console.error('加载祝福语失败:', error)
  }
}

// 初始化显示的祝福语（最多7条）
const initializeDisplayedBlessings = () => {
  displayedBlessings.value = blessings.value.slice(0, Math.min(7, blessings.value.length))
  currentIndex.value = Math.min(7, blessings.value.length)
}

// 滚动祝福语
const scrollBlessings = () => {
  if (blessings.value.length <= 7) return
  
  if (currentIndex.value >= blessings.value.length) {
    currentIndex.value = 0
  }
  
  // 给第一条添加退出动画类
  const firstItem = document.querySelector('.blessing-item:first-child')
  if (firstItem) {
    firstItem.classList.add('slide-out')
    
    // 动画结束后移除第一条，添加新的一条
    setTimeout(() => {
      displayedBlessings.value.shift()
      displayedBlessings.value.push(blessings.value[currentIndex.value])
      currentIndex.value++
    }, 500) // 与动画持续时间匹配
  } else {
    // 如果DOM元素还没准备好，使用原来的逻辑
    displayedBlessings.value.shift()
    displayedBlessings.value.push(blessings.value[currentIndex.value])
    currentIndex.value++
  }
}

// 提交新祝福语
const submitBlessing = () => {
  if (!newBlessing.value.name.trim() || !newBlessing.value.message.trim()) {
    alert('请填写姓名和祝福语')
    return
  }
  
  const newMessage = {
    id: blessings.value.length + 1,
    name: newBlessing.value.name.trim(),
    message: newBlessing.value.message.trim(),
    time: new Date().toLocaleString('zh-CN')
  }
  
  // 添加到祝福语列表
  blessings.value.push(newMessage)
  
  // 清空表单
  newBlessing.value.name = ''
  newBlessing.value.message = ''
  
  // 如果当前显示的祝福语少于7条，直接添加
  if (displayedBlessings.value.length < 7) {
    displayedBlessings.value.push(newMessage)
  }
  
  alert('祝福语提交成功！')
}

// 点击视频封面跳转到播放页面
const playVideo = (videoName) => {
  // 跳转到 video.html 并通过 URL 参数传递文件名
  const videoUrl = `/VideoAndBlessings/VideoPlayer.html?name=${encodeURIComponent(videoName)}`
  window.open(videoUrl, '_blank')
}


// 处理视频悬停效果
const handleVideoHover = (event) => {
  event.target.play()
}

const handleVideoLeave = (event) => {
  event.target.pause()
  event.target.currentTime = 0
}

// 处理视频加载完成
const handleVideoLoaded = (event) => {
  // 确保视频显示第一帧
  event.target.currentTime = 0.1
}

// 处理视频元数据加载
const handleLoadedMetadata = (event) => {
  // 设置视频显示第一帧
  event.target.currentTime = 0.1
}

onMounted(() => {
  videos.value = videoList
  loadBlessings()
  
  // 每3秒滚动一次祝福语
  scrollInterval.value = setInterval(scrollBlessings, 3000)
})

onUnmounted(() => {
  if (scrollInterval.value) {
    clearInterval(scrollInterval.value)
  }
})
</script>

<template>
  <div class="video-container">
    <div class="title-section">
      <h2 class="title">『校友之家』</h2>
    </div>
    
    <div class="content-section">
      <div class="main-content">
      <!-- 左侧视频展示区域 -->
      <div class="video-section">
        <!-- 大视频区域 -->
        <div class="featured-video" v-if="videos.length > 0">
          <div 
            class="video-card featured"
            @click="playVideo(videos[0].video)"
          >
            <div class="video-thumbnail">
              <video 
                :src="videos[0].thumbnail" 
                muted
                preload="metadata"
                class="thumbnail-video"
                @mouseenter="handleVideoHover($event)"
                @mouseleave="handleVideoLeave($event)"
                @loadeddata="handleVideoLoaded($event)"
                @loadedmetadata="handleLoadedMetadata($event)"
                poster=""
              ></video>
              <div class="play-overlay">
                <div class="play-button">▶</div>
              </div>
              <div class="video-name-overlay">
                <span class="video-name">{{ videos[0].video.split('.')[0] }}</span>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 小视频区域 -->
        <div class="small-videos-grid" v-if="videos.length > 1">
          <div 
            v-for="video in videos.slice(1, Math.min(4, videos.length))" 
            :key="video.id"
            class="video-card small"
            @click="playVideo(video.video)"
          >
            <div class="video-thumbnail">
              <video 
                :src="video.thumbnail" 
                muted
                preload="metadata"
                class="thumbnail-video"
                @mouseenter="handleVideoHover($event)"
                @mouseleave="handleVideoLeave($event)"
                @loadeddata="handleVideoLoaded($event)"
                @loadedmetadata="handleLoadedMetadata($event)"
                poster=""
              ></video>
              <div class="play-overlay">
                <div class="play-button small-play-button">▶</div>
              </div>
              <div class="video-name-overlay">
                <span class="video-name small-video-name">{{ video.video.split('.')[0] }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 右侧祝福语区域 -->
      <div class="right-section">
        <div class="blessings-container">
          <!-- 祝福语标题 -->
          <!-- <h3 class="blessings-title">祝福留言</h3> -->
          
          <!-- 祝福语滚动显示区域 -->
          <div class="blessings-display">
            <div 
              v-for="(blessing, index) in displayedBlessings" 
              :key="blessing.id"
              class="blessing-item"
              :style="{ animationDelay: `${index * 0.1}s` }"
            >
              <div class="blessing-header">
                <span class="blessing-name">{{ blessing.name }}</span>
                <span class="blessing-time">{{ blessing.time }}</span>
              </div>
              <div class="blessing-message">{{ blessing.message }}</div>
            </div>
          </div>
          
          <!-- 填写祝福语区域 -->
          <div class="blessing-form">
            <!-- <h4 class="form-title">留下你的祝福</h4> -->
            <div class="form-group">
              <textarea 
                v-model="newBlessing.message"
                placeholder="请输入您的祝福语(不超过40字)"
                class="form-textarea"
                rows="2"
              ></textarea>
            </div>
            <div class="form-row">
              <input 
                v-model="newBlessing.name"
                type="text" 
                placeholder="请输入您的姓名"
                class="form-input-inline"
              />
              <button @click="submitBlessing" class="submit-button-inline">
                为母校送上祝福
              </button>
            </div>
          </div>
        </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.video-container {
  width: 100vw;
  min-height: 100vh;
  padding: 2rem;
  margin-left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}

.video-container {
  width: 100vw;
  min-height: 100vh;
  padding: 2rem;
  margin-left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  background-image: url('/schoolfellow/background.png');
  background-size: cover;
  background-position: center;
}

.title-section {
  width: 100%;
  padding: 2rem 0;
  /* margin-bottom: 2rem;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); */
}

.title {
  text-align: center;
  font-size: 2.5rem;
  margin: 0;
  font-weight: bold;
  color: #000000;
  text-shadow: 1px 1px 3px rgba(255, 255, 255, 0.8);
}

.content-section {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 30px;
}

.main-content {
  display: flex;
  width: 100%;
  max-width: 1400px;
  gap: 3rem;
}

.video-section {
  flex: 1;
  min-width: 0;
}

.right-section {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  height: 100%;
}

.video-section {
  display: flex;
  flex-direction: column;
  gap: 2.5rem; /* 增加大视频和小视频之间的竖直间距 */
  height: 100%;
  min-height: 500px; /* 确保与右侧高度一致 */
  padding-top: 12px; /* 向下移动左边区域 */
}

.featured-video {
  width: 100%;
  flex: 2; /* 占据2/3的空间 */
  margin-top: -10px; /* 向上移动10px */
}

.small-videos-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1.5rem;
  width: 100%;
  flex: 1; /* 占据1/3的空间 */
  margin-bottom: -29px; /* 向上移动10px */
}

.video-card {
  background: #1a1a1a;
  border-radius: 15px;
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
  border: 1px solid rgba(255, 255, 255, 0.1);
  height: 100%;
}

.video-card.featured {
  height: 100%;
}

.video-card.small {
  height: 100%;
}

.video-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

.video-thumbnail {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.thumbnail-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.video-card:hover .thumbnail-video {
  transform: scale(1.05);
}

.small-play-button {
  width: 40px;
  height: 40px;
  font-size: 18px;
}

.play-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.video-card:hover .play-overlay {
  opacity: 1;
}

.play-button {
  width: 60px;
  height: 60px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: #1a1a1a;
  transition: all 0.3s ease;
}

.play-button:hover {
  background: white;
  transform: scale(1.1);
}

.video-name-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  padding: 8px;
  box-sizing: border-box;
}

.video-name {
  color: white;
  font-size: 14px;
  display: block;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-left: 10px;
}

.small-video-name {
  font-size: 12px;
}

.video-info {
  padding: 1.5rem;
  text-align: center;
}

.video-title {
  font-size: 1.2rem;
  margin: 0;
  color: rgba(255, 255, 255, 0.9);
}

.blessings-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.blessings-title {
  font-size: 1.8rem;
  color: rgb(0, 0, 0);
  text-align: center;
  margin: 0 0 1rem 0;
  font-weight: bold;
}

.blessings-display {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  max-height: 400px;
  overflow: hidden;
}

/**
祝福语默认样式
*/
.blessing-item {
  /* background: linear-gradient(135deg, #FF9966 0%, #FF5E62 100%); */
  border-color: rgba(219, 216, 25, 0.74);
  /* background: linear-gradient(135deg, rgba(212, 226, 15, 0.25) 0%, rgba(255, 255, 255, 0.1) 100%);  */
  background-color: #AC1319;
  border-radius: 10px;
  padding: 1rem;
  border: 1px solid rgba(255, 255, 255, 0.3);
  animation: slideInUp 0.5s ease-out forwards;
  opacity: 0;
  transform: translateY(20px);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(5px);
  transition: all 0.3s ease;
  cursor: pointer;
}

/**
鼠标悬浮到祝福语的样式
*/
.blessing-item:hover {
  transform: translateY(-5px) scale(1.50);
  box-shadow: 0 8px 20px rgba(146, 167, 33, 0.15);
  /* border-color: rgba(219, 216, 25, 0.74);
  background: linear-gradient(135deg, rgba(212, 226, 15, 0.25) 0%, rgba(255, 255, 255, 0.1) 100%);  */
  /* background: linear-gradient(135deg, #FF9966 0%, #FF5E62 100%); */
  /* background: linear-gradient(135deg, #e7e43fe3 0%, #FF5E62 100%); */
  background-color: #c0161b;
}

.blessing-item.slide-out {
  animation: slideOutUp 0.5s ease-out forwards;
}

.blessing-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.blessing-name {
  color: #ffffff;
  font-weight: bold;
  font-size: 1rem;
}

.blessing-time {
  color: rgba(255, 255, 255, 0.8);
  font-size: 0.8rem;
}

.blessing-message {
  color: #ffffff;
  font-size: 0.9rem;
  line-height: 1.4;
}

.blessing-form {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 15px;
  padding: 0rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
  margin-top: 0rem;
}

.form-title {
  color: rgb(0, 0, 0);
  font-size: 1.2rem;
  margin: 0 0 1rem 0;
  text-align: center;
}

.form-group {
  margin-bottom: 1rem;
  /* background-color: white; */
  border-radius: 8px;
  
}

.form-textarea {
  width: 100%;
  padding: 0.6rem;
  border: 1px solid #ccc;
  border-radius: 8px;
  background: white;
  color: rgb(0, 0, 0);
  font-size: 1rem;
  line-height: 1.4;
  resize: none;
  overflow: hidden;
  box-sizing: border-box;
  min-height: 60px; /* 减小最小高度 */
  height: 60px; /* 固定高度 */
  transition: border-color 0.3s, box-shadow 0.3s;
}

.form-input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  margin-bottom: 10px;
}

/* 输入框的焦点样式 */
.form-textarea:focus,
.form-input-inline:focus {
  outline: none;
  border: 1px solid #F9BE0D;
  box-shadow: 0 0 5px rgba(76, 175, 80, 0.2);
}

.form-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;
}

.form-input-inline {
  flex: 1;
  padding: 0.8rem;
  border: 1px solid #ccc;
  border-radius: 8px;
  background: white;
  margin-right: 15px;
  font-size: 1rem;
  line-height: 1.5;
  box-sizing: border-box;
}

.submit-button-inline {
  padding: 0.8rem 1.2rem;
  background-color: #FBCF4D;
  color: rgb(255, 255, 255);
  font-weight: bold;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1.5;
}

.submit-button {
  width: 100%;
  padding: 0.8rem;
  background: linear-gradient(45deg, #667eea 0%, #764ba2 100%);
  color: rgb(255, 255, 255);
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
}

.submit-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

.form-button-container {
  display: flex;
  justify-content: center;
  margin-top: 1rem;
}

.form-input {
  width: 100%;
  padding: 0.8rem;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.1);
  color: rgb(0, 0, 0);
  font-size: 0.9rem;
  box-sizing: border-box;
}

.form-input::placeholder,
.form-textarea::placeholder,
.form-input-inline::placeholder {
  color: rgba(0, 0, 0, 0.6);
  font-family: "Microsoft YaHei", "PingFang SC", sans-serif;
  font-size: 0.9rem;
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes slideOutUp {
  from {
    opacity: 1;
    transform: translateY(0);
  }
  to {
    opacity: 0;
    transform: translateY(-20px);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .main-content {
    flex-direction: column;
    gap: 2rem;
  }
  
  .video-grid {
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
  }
  
  .title {
    font-size: 2rem;
  }
  
  .video-container {
    padding: 1rem;
  }
}
</style>